

package com.hazelcast.internal.serialization.impl.defaultserializers;

import com.hazelcast.internal.serialization.impl.SerializationConstants;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;

import java.io.IOException;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * The {@link TreeMap} serializer
 */
public class TreeMapStreamSerializer<K, V> extends AbstractMapStreamSerializer<TreeMap<K, V>> {
    @Override
    public int getTypeId() {
        return SerializationConstants.JAVA_DEFAULT_TYPE_TREE_MAP;
    }

    @Override
    public void write(ObjectDataOutput out, TreeMap<K, V> map) throws IOException {
        out.writeObject(((TreeMap<K, V>) map).comparator());

        super.write(out, map);
    }

    @Override
    public TreeMap<K, V> read(ObjectDataInput in) throws IOException {
        Comparator<? super K> comparator = in.readObject();

        TreeMap<K, V> map = new TreeMap<>(comparator);

        int size = in.readInt();

        return deserializeEntries(in, size, map);
    }
}
